package cn.com.callback

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.kh.KHUtil
import cn.com.kh.LogToDB
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * @projectName: ngsapitask-sw
 * @className: apiLocalWOSend
 * @author: LiJT
 * @description: 宝安&光明工单下送时获取下送人API
 * @date: 2022-09-27 00:12
 * @version: 1.0
 */

public class apiLocalWOSend implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiLocalWOSend.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        // 获取数据源List<Map<String, Object>>
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        String getMethod = request.getMethod();
        log.debug("宝安&光明工单下送时获取下送人-getMethod:" + getMethod);
        long startTime = System.currentTimeMillis();

        String inData = "";
        JSONObject outData = new JSONObject();

        if (getMethod.equals("GET")) {
            inData = request.getParameter("data");
            log.debug("宝安&光明工单下送时获取下送人-原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("宝安&光明工单下送时获取下送人-原始Post请求参数:" + inData);
        }
        String r_workorder_id = "";
        try {
            // 解析短信结果推送
            JSONObject getPostJson = JSONObject.fromObject(inData);

            // 获取调用参数
            r_workorder_id = getPostJson.getString("workorder_id");
            String r_area_id = getPostJson.getJSONObject("data").getString("area_id");
            String r_company_id = getPostJson.getJSONObject("data").getString("company_id");
            String r_wo_type1_id = getPostJson.getJSONObject("data").getString("wo_type1_id");
            String r_wo_type2_id = getPostJson.getJSONObject("data").getString("wo_type2_id");
            String r_wo_type3_id = getPostJson.getJSONObject("data").getString("wo_type3_id");

            // 控制规则查询逻辑
            String where1SQL = " And R1.Area_Id = '" + r_area_id + "'";
            log.debug("控制规则查询逻辑1:" + where1SQL);
            String where2SQL = KHUtil.isNull(r_company_id) ? " And (R1.Company_Id = '' Or R1.Company_Id Is Null)" : " And (R1.Company_Id = '" + r_company_id + "' Or R1.Company_Id = '' Or R1.Company_Id Is Null)";
            log.debug("控制规则查询逻辑2:" + where2SQL);
            where1SQL = where1SQL + where2SQL;
            log.debug("控制规则查询逻辑:" + where1SQL);

            String whereSQL = "";
            List<Map<String, Object>> selectList = null;
            if (!KHUtil.isNull(r_wo_type3_id)) {
                whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And R1.Wo_Type2_Id = '" + r_wo_type2_id + "' And R1.Wo_Type3_Id = '" + r_wo_type3_id + "'";
                selectList = getDBConfig(jt, whereSQL);
                log.debug("3-3级选项匹配规则结果数量:" + selectList.size());
                if (selectList.size() <= 0) {
                    whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And R1.Wo_Type2_Id = '" + r_wo_type2_id + "' And (R1.Wo_Type3_Id = '' Or R1.Wo_Type3_Id Is Null)";
                    selectList = getDBConfig(jt, whereSQL);
                    log.debug("3-2级选项匹配规则结果数量:" + selectList.size());

                    if (selectList.size() <= 0) {
                        whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And (R1.Wo_Type2_Id = '' Or R1.Wo_Type2_Id Is Null) And (R1.Wo_Type3_Id = '' Or R1.Wo_Type3_Id Is Null)";
                        selectList = getDBConfig(jt, whereSQL);
                        log.debug("3-1级选项匹配规则结果数量:" + selectList.size());

                    }
                }

            } else if (!KHUtil.isNull(r_wo_type2_id)) {
                whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And R1.Wo_Type2_Id = '" + r_wo_type2_id + "' And (R1.Wo_Type3_Id = '' Or R1.Wo_Type3_Id Is Null)";
                selectList = getDBConfig(jt, whereSQL);
                log.debug("2-2级选项匹配规则结果数量:" + selectList.size());

                if (selectList.size() <= 0) {
                    whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And (R1.Wo_Type2_Id = '' Or R1.Wo_Type2_Id Is Null) And (R1.Wo_Type3_Id = '' Or R1.Wo_Type3_Id Is Null)";
                    selectList = getDBConfig(jt, whereSQL);
                    log.debug("2-1级选项匹配规则结果数量:" + selectList.size());

                }
            } else if (!KHUtil.isNull(r_wo_type1_id)) {
                whereSQL = where1SQL + " And R1.Wo_Type1_Id = '" + r_wo_type1_id + "' And (R1.Wo_Type2_Id = '' Or R1.Wo_Type2_Id Is Null) And (R1.Wo_Type3_Id = '' Or R1.Wo_Type3_Id Is Null)";
                selectList = getDBConfig(jt, whereSQL);
                log.debug("1-1级选项匹配规则结果数量:" + selectList.size());
            } else {
                selectList = [];
                log.debug("0-0级选项匹配规则结果数量:" + selectList.size());
            }

            // 匹配结果识别
            outData.put("code", "1");
            outData.put("msg", "宝安&光明工单下送时获取下送人-处理完成");
            if (selectList.size() == 1) {
                outData.put("Rec_Guid", selectList.get(0).get("Rec_Guid"));
                outData.put("Rolegroup_Id", selectList.get(0).get("Rolegroup_Id"));
                outData.put("Staff_Id", selectList.get(0).get("Staff_Id"));
                outData.put("Mobile", selectList.get(0).get("Mobile"));
                outData.put("Staffname", selectList.get(0).get("Staffname"));
            } else if (selectList.size() > 1) {
                outData.put("Rec_Guid", selectList.get(0).get("Rec_Guid"));
                outData.put("Rolegroup_Id", selectList.get(0).get("Rolegroup_Id"));

                String MobileInfo = "";
                String StaffnameInfo = "";
                for (int i = 0; i < selectList.size(); i++) {
                    log.debug("循环规则结果:" + selectList.get(i));
                    String Mobile = selectList.get(i).get("Mobile");
                    String Staffname = selectList.get(i).get("Staffname");
                    Staffname = KHUtil.isNull(Staffname) ? Mobile : Staffname;
                    log.debug("循环规则结果Mobile:" + Mobile);
                    log.debug("循环规则结果Staffname:" + Staffname);

                    if (!KHUtil.isNull(Mobile)) {
                        if (KHUtil.isNull(StaffnameInfo)) {
                            MobileInfo = MobileInfo.concat(Mobile);
                            StaffnameInfo = StaffnameInfo.concat(Staffname);
                        } else {
                            MobileInfo = MobileInfo.concat(",").concat(Mobile);
                            StaffnameInfo = StaffnameInfo.concat(",").concat(Staffname);
                        }
                        log.debug("循环规则结果Mobile2:" + MobileInfo);
                        log.debug("循环规则结果Staffname2:" + StaffnameInfo);
                    }
                }
                outData.put("Staff_Id", "");
                outData.put("Mobile", MobileInfo);
                outData.put("Staffname", StaffnameInfo);

            } else {

                // 失败返回
                outData.put("msg", "宝安&光明工单下送时获取下送人-未获取到直接处理人");
                outData.put("Rec_Guid", "");
                outData.put("Rolegroup_Id", "");
                outData.put("Staff_Id", "");
                outData.put("Mobile", "");
                outData.put("Staffname", "");

            }

        } catch (Exception e) {
            log.error("宝安&光明工单下送时获取下送人-接口处理异常" + e.getMessage());
            // 失败返回
            outData.put("code", "-1");
            outData.put("msg", e.getMessage());
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "宝安&光明工单下送时获取下送人[普通]", "apiLocalWOSend", inData, outData.toString(), (System.currentTimeMillis() - startTime).toString(), r_workorder_id);
        }

        log.debug("宝安&光明工单下送时获取下送人-处理完成返回结果:" + outData.toString());

        return outData.toString();
    }

    // 获取查询结果
    public static List<Map<String, Object>> getDBConfig(JdbcTemplate jt, String whereSQL) {

        String selectSQL = '''Select R1.Rec_Guid,
                               R1.Rolegroup_Id,
                               Isnull(R1.Staff_Id, '') As Staff_Id,
                               Isnull(R5.Mobile, '') As Mobile,
                               R1.Area_Id,
                               R2.Area_Desc,
                               R1.Company_Id,
                               R3.Code_Name,
                               R1.Wo_Type1_Id,
                               C1.Code_Name,
                               R1.Wo_Type2_Id,
                               C2.Code_Name,
                               R1.Wo_Type3_Id,
                               C3.Code_Name,
                               R4.Rolegroupdescription,
                               Isnull(R5.Staffname, '') As Staffname
                          From Szwg_Wo_Sendstaff_Config R1
                          Left Join Group_Area R2
                            On R1.Area_Id = R2.Area_Id
                          Left Join c_Codetables R3
                            On R1.Company_Id = R3.Code_Id
                           And R1.Area_Id = R3.Code_Desc
                           And R3.Use_Type = '受理部门\'
                          Left Join c_Codetables C1
                            On R1.Wo_Type1_Id = C1.Code_Id
                           And C1.Use_Type = '一级业务类型\'
                           And C1.Use_Status = '1\'
                          Left Join c_Codetables C2
                            On R1.Wo_Type2_Id = C2.Code_Id
                           And R1.Wo_Type1_Id = C2.Pcode_Id
                           And C2.Use_Type = '二级业务类型\'
                           And C2.Use_Status = '1\'
                          Left Join c_Codetables C3
                            On R1.Wo_Type3_Id = C3.Code_Id
                           And R1.Wo_Type2_Id = C3.Pcode_Id
                           And C3.Use_Type = '三级业务类型\'
                           And C3.Use_Status = '1\'
                          Left Join Rolegroup R4
                            On R1.Rolegroup_Id = R4.Rolegroup_Id
                          Left Join Staff R5
                            On R1.Staff_Id = R5.Staff_Id
                         Where 1 = 1''';

        selectSQL = selectSQL + whereSQL;

        log.debug("查询规则配置selectSQL:" + selectSQL);
        List<Map<String, Object>> selectList = jt.queryForList(selectSQL);
        log.debug("查询规则结果selectList:" + selectList);

        return selectList;
    }
}